{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "11ce6bff-4785-430a-866b-2bdb17a23085",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 为了减少变量命名复杂性，每个分割线间使用的变量名虽然相同但含义无关。\n",
    "# ————————————————————————————————————————————————————————————————\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b6ddb2bf-36c4-429a-9d73-8d8e69c24e33",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建向量和数组、指定类型，注意不同维度的ndarry形状\n",
    "v = np.array([1,2,3,4,5,6],dtype='float64')\n",
    "arr2 = np.array([[1,2],[3,4],[5,6]],dtype='float64')\n",
    "arr3 = np.array([[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]]],dtype='float64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6118ad9e-3f69-46b7-8ea3-e908d5b10e44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1., 2., 3., 4., 5., 6.]),\n",
       " array([[1., 2.],\n",
       "        [3., 4.],\n",
       "        [5., 6.]]),\n",
       " array([[[1., 2., 3., 4.],\n",
       "         [1., 2., 3., 4.]],\n",
       " \n",
       "        [[1., 2., 3., 4.],\n",
       "         [1., 2., 3., 4.]]]))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v,arr2,arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "125072ae-2ef9-4407-a97f-5db40155bfd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((6,), (3, 2), (2, 2, 4))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v.shape,arr2.shape,arr3.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "935ba83c-a6cb-4ae7-bc8b-57f75fd09475",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[   1.,    2.,    3.],\n",
       "        [   4., 1000.,    6.]]),\n",
       " array([   1.,    2.,    3.,    4., 1000.,    6.]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组形状重塑，明确在Numpy中对数组的操作通常产生视图（共享内存，更改影响原数组）\n",
    "# 想要避免这个情况可以使用np.copy()显式定义一个副本\n",
    "t = v.reshape((2,-1))\n",
    "t[1,1] = 1000\n",
    "t,v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f68c5c0e-aa25-4088-bbdd-0fa22dc007ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[   1.,    2.,    3.],\n",
       "        [   4., 1000.,    6.]]),\n",
       " array([[1., 2.],\n",
       "        [3., 4.],\n",
       "        [5., 6.]]))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = arr2.copy().reshape((2,-1))\n",
    "t1[1,1] = 1000\n",
    "t1,arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "43de0d8b-4296-4c43-b67f-5fb0bc806dbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ————————————————————————————————————————————————————————————————————————\n",
    "# 创建行向量、列向量、矩阵\n",
    "v = np.array([1,2,3,4])\n",
    "v1 = np.array([1,2,3,4]).reshape((-1,1))\n",
    "arr2 = np.array([[1,2,3],[4,5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "901f3701-c025-499f-8dae-80665a22104c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 2, 3, 4]),\n",
       " array([[1],\n",
       "        [2],\n",
       "        [3],\n",
       "        [4]]),\n",
       " array([[1, 2, 3],\n",
       "        [4, 5, 6]]))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v,v1,arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ee651d56-75a5-4432-8a67-6d7a69391d00",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建数组的几种方法：递增数组、常数数组、随机数组(0-1小数分布、整数随机数、正态分布)\n",
    "arr3 = np.arange(0,100,2) # 参数分别为(开始点、结束点后一个、步长)\n",
    "arr4 = np.ones((3,4))\n",
    "arr5 = np.zeros((4,3))\n",
    "arr6 = np.pi*np.zeros((4,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c77beb2f-78ef-44e5-8408-9d1b6162ec8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32,\n",
       "        34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66,\n",
       "        68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]),\n",
       " array([[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]]),\n",
       " array([[0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]]),\n",
       " array([[0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]]))"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3,arr4,arr5,arr6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1ff02b7b-720e-4cdc-859c-291c7da7abb7",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr7 = np.random.random((3,5)) # 创建[0,1]均匀分布数组\n",
    "arr8 = np.random.randint(10,20,(3,4)) # 参数分别为(开始点、结束点后一个、形状)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "87ac56e0-8eb3-46ac-afc2-47ab2bc9c911",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[0.14386933, 0.96922124, 0.01991458, 0.24690706, 0.799708  ],\n",
       "        [0.72481232, 0.82470731, 0.65837529, 0.46425882, 0.36954367],\n",
       "        [0.79687738, 0.18352712, 0.21890035, 0.32649142, 0.80647359]]),\n",
       " array([[18, 12, 17, 14],\n",
       "        [14, 17, 12, 13],\n",
       "        [13, 11, 18, 12]]))"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr7,arr8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b02e9350-a8df-40b3-b097-ee17606d2e6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ -7.53770234,  15.58779245,  27.87106466, -29.62639629,\n",
       "         11.28480699],\n",
       "       [-12.35723315,  -5.31193842,   6.02190784,  -8.68066969,\n",
       "         25.77488097],\n",
       "       [ -6.6402103 ,  -7.92690761,  18.28138211,  12.3220159 ,\n",
       "          4.3257295 ]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr9 = np.random.normal(10,20,(3,5)) # 参数分别为(均值、方差、形状)\n",
    "arr9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d9b6c27c-dd85-42c7-998b-4e8a96b68d92",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ————————————————————————————————————————————————————————————————————————\n",
    "# 访问数组元素方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e4aa1443-f3ae-4155-a46b-760ab525450b",
   "metadata": {},
   "outputs": [],
   "source": [
    "v = np.arange(20)\n",
    "arr1 = np.random.random((30,50)) # 创建[0,1]均匀分布数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e2c3f81a-a033-4573-8c34-834640a77a62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 19, 0.694647629743408)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下标元组访问法，-1为最后一个\n",
    "v[1],v[-1],arr1[1,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "30e55895-50c8-4ddd-af39-053c53fb3796",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18,\n",
       "        19]),\n",
       " array([[5.18390997e-01, 7.62637622e-01, 7.66194499e-01, 4.60889840e-01,\n",
       "         6.71193835e-01, 5.17120050e-01, 7.01161971e-01, 5.55024728e-01,\n",
       "         9.15300004e-02, 8.28619439e-01, 6.38308155e-01],\n",
       "        [7.20689694e-01, 3.49868462e-01, 2.10044852e-01, 8.48404928e-01,\n",
       "         4.27321955e-01, 3.05253160e-01, 9.55192335e-01, 8.74150721e-01,\n",
       "         1.98521235e-01, 4.65392774e-01, 8.28651039e-01],\n",
       "        [6.54511203e-01, 2.14633223e-01, 9.26403203e-01, 6.99997993e-03,\n",
       "         2.59958926e-01, 2.72850457e-01, 9.44569777e-02, 7.06695417e-01,\n",
       "         6.28496153e-01, 5.92060000e-01, 2.19418511e-01],\n",
       "        [6.57648216e-01, 8.14516885e-02, 2.41327012e-01, 9.69054156e-01,\n",
       "         7.37963340e-01, 8.72677036e-01, 3.47887198e-01, 9.77368682e-01,\n",
       "         3.15385419e-01, 4.93935641e-01, 9.27263115e-01],\n",
       "        [2.50449225e-01, 2.11856125e-01, 8.84042031e-01, 1.96542118e-01,\n",
       "         1.98328535e-02, 6.84571484e-01, 7.57578820e-01, 9.70015424e-01,\n",
       "         1.64685192e-01, 8.77549860e-01, 4.51314114e-01],\n",
       "        [3.92191993e-01, 3.89732581e-01, 2.80816812e-02, 8.25278180e-01,\n",
       "         8.95372319e-01, 5.77455923e-01, 7.12893846e-01, 2.31226404e-01,\n",
       "         2.50403424e-01, 6.76488748e-01, 5.50487649e-01],\n",
       "        [1.61289604e-01, 5.28751394e-01, 7.29941423e-01, 8.85174724e-01,\n",
       "         9.03615415e-01, 1.82349330e-01, 3.90851932e-01, 9.02309399e-01,\n",
       "         6.16145124e-02, 6.25328930e-01, 7.37245914e-01],\n",
       "        [1.83106966e-01, 2.23067929e-01, 4.76879889e-01, 2.07581057e-01,\n",
       "         5.17194984e-01, 5.04320500e-01, 8.90803659e-02, 2.88980624e-02,\n",
       "         5.59610344e-01, 6.85432966e-01, 3.69707301e-05],\n",
       "        [1.39117442e-02, 6.64141099e-01, 1.24770413e-01, 7.07734315e-01,\n",
       "         5.04846522e-01, 2.29257753e-01, 5.26864593e-01, 7.08367412e-01,\n",
       "         2.34301628e-01, 4.78460689e-01, 2.96591768e-02],\n",
       "        [9.59479723e-01, 3.82784344e-01, 2.35882496e-01, 7.29145243e-01,\n",
       "         1.86013165e-01, 6.72030119e-01, 3.49015125e-01, 2.49573599e-02,\n",
       "         7.80651289e-01, 4.28081032e-01, 3.31072638e-01],\n",
       "        [7.74638277e-01, 9.16558585e-01, 6.59325132e-01, 5.29565655e-01,\n",
       "         4.47241311e-01, 2.27599431e-01, 9.34236752e-01, 9.79898868e-01,\n",
       "         4.05152961e-01, 5.93833799e-01, 1.19617593e-02]]),\n",
       " array([17, 18, 19]),\n",
       " array([[1.90347323e-02, 9.30358976e-01, 7.12636704e-01, 7.35057446e-01,\n",
       "         6.83548646e-01, 1.49790345e-01, 6.54582956e-01, 9.67467432e-01,\n",
       "         6.34075139e-01, 2.60610533e-01, 9.94649055e-01, 8.65820587e-01,\n",
       "         8.07601880e-01, 3.38901282e-01, 2.15841651e-01, 8.49826276e-01,\n",
       "         9.62516251e-01, 7.26060316e-01, 8.35526210e-01, 6.53005860e-01,\n",
       "         6.18783355e-01, 4.19210892e-01, 3.09108058e-01, 7.20186749e-01,\n",
       "         1.45445551e-01, 5.90179776e-01, 1.91000133e-02, 6.68066260e-01,\n",
       "         4.37543242e-01, 2.43893375e-01, 6.36928954e-01, 9.27207425e-02,\n",
       "         4.57315805e-02, 9.33798416e-02, 4.58019791e-01, 9.50345803e-01,\n",
       "         5.05260528e-01, 5.63735633e-01, 6.95693350e-01, 2.33172780e-01,\n",
       "         7.28618069e-01, 7.05251735e-01, 2.49036225e-02, 8.33842759e-01,\n",
       "         4.75957407e-01, 9.21700510e-01, 8.93378173e-01, 6.89466527e-01,\n",
       "         7.84088705e-02, 2.47747497e-01],\n",
       "        [3.96040910e-01, 7.77005622e-01, 4.03188282e-01, 5.46032974e-01,\n",
       "         2.36006462e-01, 3.53332887e-01, 2.19868261e-01, 4.74398411e-01,\n",
       "         5.57984505e-02, 5.97036981e-01, 8.44347080e-01, 8.59400350e-01,\n",
       "         3.37266248e-01, 1.25712267e-01, 3.75887766e-01, 5.64613654e-01,\n",
       "         3.80940246e-01, 8.11168501e-01, 9.83478880e-02, 9.31564126e-01,\n",
       "         8.39412190e-01, 3.16469584e-01, 7.26728914e-01, 1.38420041e-01,\n",
       "         5.99852731e-01, 9.57320039e-01, 3.33914084e-01, 8.00948367e-01,\n",
       "         7.63297321e-01, 4.30109181e-01, 7.90175043e-01, 4.48909790e-01,\n",
       "         8.66219089e-01, 7.12242301e-01, 8.93093919e-01, 7.05214868e-01,\n",
       "         3.50547973e-01, 8.54809769e-01, 1.24449831e-01, 9.70334367e-01,\n",
       "         8.74190596e-03, 1.53976322e-02, 6.36803556e-01, 6.92461871e-01,\n",
       "         1.09582875e-01, 2.33560829e-01, 3.57981216e-01, 5.99069960e-01,\n",
       "         5.27286905e-01, 6.66489317e-01],\n",
       "        [3.70723697e-02, 6.49119367e-02, 6.26616185e-01, 6.85632525e-01,\n",
       "         4.65027638e-01, 7.04261587e-02, 4.84601797e-02, 2.25662826e-01,\n",
       "         5.42048026e-01, 8.60329905e-01, 9.43824402e-04, 7.96950892e-01,\n",
       "         1.46188083e-01, 3.73669828e-01, 6.29133674e-01, 2.81699270e-01,\n",
       "         1.08120533e-01, 4.30177262e-02, 3.80853244e-01, 9.35598133e-01,\n",
       "         1.31850171e-01, 7.29628921e-01, 8.90241930e-01, 8.94497834e-01,\n",
       "         5.82240347e-01, 8.43814401e-01, 1.13528240e-02, 5.77300405e-01,\n",
       "         5.20251978e-01, 7.27273828e-01, 8.67641399e-01, 6.87365401e-01,\n",
       "         3.04651464e-01, 7.49797354e-01, 2.60165034e-01, 2.77954307e-01,\n",
       "         4.94163173e-01, 1.85038918e-01, 9.74567176e-01, 7.91783428e-01,\n",
       "         6.31659398e-01, 6.26599001e-01, 7.67343981e-01, 3.70424374e-01,\n",
       "         3.25936626e-01, 2.69614517e-01, 5.75911724e-01, 1.14063505e-01,\n",
       "         8.94527619e-01, 7.65061343e-01]]),\n",
       " array([[6.27588760e-01, 5.55938200e-01, 1.69220685e-01, 8.23388801e-05,\n",
       "         5.79079054e-02, 4.27636221e-01, 8.38414612e-01, 3.39209821e-01,\n",
       "         8.37369444e-01],\n",
       "        [3.50076896e-01, 8.61016177e-01, 6.08054749e-01, 1.28408330e-02,\n",
       "         4.94574157e-01, 1.11761493e-01, 5.77632713e-02, 9.64328716e-01,\n",
       "         6.90755643e-01],\n",
       "        [4.57536880e-01, 8.64234012e-01, 6.03686600e-01, 9.46204795e-02,\n",
       "         8.31739441e-01, 4.18561657e-01, 2.53610649e-01, 9.56599082e-01,\n",
       "         2.04191384e-01],\n",
       "        [7.38269823e-01, 4.66796798e-01, 7.38118153e-01, 6.56251963e-01,\n",
       "         8.94391053e-01, 2.58110367e-01, 5.14637516e-02, 9.32314895e-01,\n",
       "         6.32570448e-01],\n",
       "        [6.57817318e-01, 1.53291589e-01, 7.17272157e-01, 1.29590333e-01,\n",
       "         1.28390563e-01, 4.01002459e-01, 9.72614781e-01, 9.40456188e-01,\n",
       "         3.03962687e-01],\n",
       "        [7.08789474e-01, 7.62511225e-01, 6.84040817e-01, 3.77516745e-02,\n",
       "         3.97752014e-02, 7.73556387e-01, 6.86981817e-01, 4.21223175e-02,\n",
       "         1.86463076e-01],\n",
       "        [7.04465979e-01, 7.88763891e-01, 6.61521310e-01, 5.44833678e-01,\n",
       "         1.69138774e-01, 2.89476106e-01, 6.40454257e-01, 5.40153490e-01,\n",
       "         4.83173388e-02],\n",
       "        [6.53442929e-01, 3.18734262e-01, 7.43189173e-02, 6.53911461e-01,\n",
       "         2.53621572e-01, 5.15917069e-01, 1.11855941e-01, 4.06818117e-01,\n",
       "         3.06013364e-01],\n",
       "        [6.32893822e-01, 2.65130908e-02, 3.81483616e-01, 5.82782879e-01,\n",
       "         8.42868682e-02, 8.78607036e-01, 2.13069617e-02, 6.74717950e-01,\n",
       "         8.45943180e-01],\n",
       "        [6.87110674e-01, 7.20492881e-01, 2.66976952e-01, 2.37885148e-01,\n",
       "         1.33416993e-01, 7.74122202e-02, 8.83869170e-02, 1.95461443e-02,\n",
       "         7.35769256e-01],\n",
       "        [2.36232049e-01, 9.72378050e-01, 8.02112750e-01, 2.57938447e-01,\n",
       "         6.58375374e-01, 4.09828499e-01, 2.76334122e-01, 2.76101730e-01,\n",
       "         5.68221685e-01],\n",
       "        [2.34128985e-01, 4.01466647e-01, 8.27338426e-01, 4.90616506e-01,\n",
       "         8.90282633e-01, 6.12360230e-01, 2.32099929e-01, 9.81576465e-01,\n",
       "         4.11505902e-01],\n",
       "        [6.63527851e-01, 1.77740980e-01, 7.50097693e-01, 2.22457419e-01,\n",
       "         1.29248512e-01, 2.83504478e-01, 9.07485937e-01, 7.28400648e-01,\n",
       "         7.67102377e-01],\n",
       "        [6.59154238e-01, 7.78438396e-01, 8.82646286e-01, 2.78167123e-01,\n",
       "         9.66516106e-01, 5.01066279e-01, 5.47665458e-02, 1.15304291e-01,\n",
       "         2.75716077e-01],\n",
       "        [6.94967534e-01, 1.58808741e-01, 5.18729043e-01, 9.42135906e-01,\n",
       "         2.77087459e-01, 3.47016218e-01, 7.06427470e-01, 6.86694274e-01,\n",
       "         7.41661321e-01],\n",
       "        [6.77754941e-01, 5.70814677e-01, 8.45739840e-01, 8.43370815e-01,\n",
       "         8.00580517e-01, 3.09545554e-01, 1.23094978e-01, 1.60305418e-01,\n",
       "         6.68380392e-01],\n",
       "        [2.39054174e-01, 4.73577536e-01, 3.90981664e-01, 6.91406078e-02,\n",
       "         2.93095793e-01, 9.21510316e-01, 5.66044356e-01, 6.83523621e-01,\n",
       "         9.59113273e-01],\n",
       "        [2.92153692e-01, 7.48672868e-01, 3.36197076e-01, 8.20644943e-01,\n",
       "         8.74323324e-01, 8.58366051e-01, 1.03313759e-02, 2.78215301e-01,\n",
       "         5.67564137e-01],\n",
       "        [5.09711150e-01, 3.06963630e-01, 7.42626506e-01, 7.35401416e-01,\n",
       "         8.41503173e-01, 4.63292176e-01, 2.93615979e-03, 3.30858115e-01,\n",
       "         2.84991849e-01],\n",
       "        [5.51404744e-02, 5.48828311e-01, 2.70786432e-01, 6.96955879e-01,\n",
       "         6.58903701e-01, 2.28394497e-01, 3.72370714e-01, 9.56992492e-01,\n",
       "         5.10170968e-01],\n",
       "        [3.13465498e-02, 9.41922556e-01, 8.54240306e-02, 5.83354520e-01,\n",
       "         3.43323212e-01, 6.51467144e-01, 8.72032992e-01, 1.95980443e-01,\n",
       "         1.64516586e-01],\n",
       "        [1.19058748e-01, 4.06949002e-01, 7.13704818e-01, 7.90749624e-01,\n",
       "         3.87164379e-01, 9.74125924e-01, 4.39111619e-01, 2.46430880e-02,\n",
       "         4.59616386e-01],\n",
       "        [4.96494188e-01, 5.85086563e-01, 1.54505019e-01, 9.99085146e-01,\n",
       "         1.47198337e-01, 1.64780278e-01, 8.65133822e-01, 4.69013734e-01,\n",
       "         9.44982634e-01],\n",
       "        [5.46437916e-01, 1.69712649e-01, 9.22724904e-01, 7.39347645e-01,\n",
       "         2.95325134e-01, 6.96441376e-01, 5.30079504e-01, 2.09220309e-01,\n",
       "         3.72606504e-01],\n",
       "        [7.03589169e-01, 9.66293840e-02, 6.35793361e-01, 5.13240023e-01,\n",
       "         1.62648327e-01, 9.94609643e-01, 8.55829416e-01, 3.51613314e-01,\n",
       "         6.60117189e-01],\n",
       "        [1.16612528e-01, 7.86873822e-01, 7.95244702e-01, 2.96971616e-01,\n",
       "         3.43975682e-01, 5.60471419e-01, 4.27162225e-02, 7.25274369e-02,\n",
       "         9.53980298e-01],\n",
       "        [5.88163218e-01, 6.98251118e-01, 9.24140190e-01, 7.88283432e-01,\n",
       "         8.79590801e-01, 1.08068405e-01, 1.12274284e-03, 3.04168575e-01,\n",
       "         5.08180683e-01],\n",
       "        [7.05251735e-01, 2.49036225e-02, 8.33842759e-01, 4.75957407e-01,\n",
       "         9.21700510e-01, 8.93378173e-01, 6.89466527e-01, 7.84088705e-02,\n",
       "         2.47747497e-01],\n",
       "        [1.53976322e-02, 6.36803556e-01, 6.92461871e-01, 1.09582875e-01,\n",
       "         2.33560829e-01, 3.57981216e-01, 5.99069960e-01, 5.27286905e-01,\n",
       "         6.66489317e-01],\n",
       "        [6.26599001e-01, 7.67343981e-01, 3.70424374e-01, 3.25936626e-01,\n",
       "         2.69614517e-01, 5.75911724e-01, 1.14063505e-01, 8.94527619e-01,\n",
       "         7.65061343e-01]]))"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片访问法，注意最后三种,可以方便的访问后XX个/行/列元素\n",
    "v[2:],arr1[1:22:2,10:43:3],v[-3:],arr1[-3:,:],arr1[:,-9:]\n",
    "# 尤其注意，切片仅为视图，不为副本，要使用copy方法显式创建副本！！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "6c2b5801-cef2-47f7-841b-29178a566ea9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1, 3, 5]), array([0.74053188, 0.16071568, 0.28858559]))"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 匹配访问法：列表套列表，对应\n",
    "v[[1,3,5]],arr1[[1,3,7],[2,3,1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "33c64634-8f5b-45f8-b592-a4d78f0e63d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ————————————————————————————————————————————————————————————————————————\n",
    "# 数组的变形：转置、上下左右反转、拼接、分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "bc7802b5-522e-4e1f-91c8-ca6644360c87",
   "metadata": {},
   "outputs": [],
   "source": [
    "v = np.arange(20)\n",
    "arr1 = np.random.random((30,50)) # 创建[0,1]均匀分布数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "88e27e4f-f129-4497-910a-6ab3db299f47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.14908129, 0.47694602, 0.80508137, ..., 0.42209953, 0.53165527,\n",
       "        0.89983688],\n",
       "       [0.929828  , 0.17350347, 0.8482673 , ..., 0.67781044, 0.3717876 ,\n",
       "        0.47149089],\n",
       "       [0.83326334, 0.03766902, 0.12417675, ..., 0.78396955, 0.7470969 ,\n",
       "        0.65183677],\n",
       "       ...,\n",
       "       [0.17954586, 0.43732438, 0.56738328, ..., 0.48773782, 0.28631926,\n",
       "        0.33412181],\n",
       "       [0.05745634, 0.79409029, 0.50978853, ..., 0.28792122, 0.28385361,\n",
       "        0.369037  ],\n",
       "       [0.90740887, 0.06445678, 0.03062591, ..., 0.51399001, 0.48664538,\n",
       "        0.35104955]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "82ca33ae-a90d-47c5-af95-f789b500063c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.90740887, 0.05745634, 0.17954586, ..., 0.83326334, 0.929828  ,\n",
       "        0.14908129],\n",
       "       [0.06445678, 0.79409029, 0.43732438, ..., 0.03766902, 0.17350347,\n",
       "        0.47694602],\n",
       "       [0.03062591, 0.50978853, 0.56738328, ..., 0.12417675, 0.8482673 ,\n",
       "        0.80508137],\n",
       "       ...,\n",
       "       [0.51399001, 0.28792122, 0.48773782, ..., 0.78396955, 0.67781044,\n",
       "        0.42209953],\n",
       "       [0.48664538, 0.28385361, 0.28631926, ..., 0.7470969 , 0.3717876 ,\n",
       "        0.53165527],\n",
       "       [0.35104955, 0.369037  , 0.33412181, ..., 0.65183677, 0.47149089,\n",
       "        0.89983688]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[::,::-1] # 左右反转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "53a52cf9-a13e-4e4c-a37b-ecedb0754665",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.89983688, 0.47149089, 0.65183677, ..., 0.33412181, 0.369037  ,\n",
       "        0.35104955],\n",
       "       [0.53165527, 0.3717876 , 0.7470969 , ..., 0.28631926, 0.28385361,\n",
       "        0.48664538],\n",
       "       [0.42209953, 0.67781044, 0.78396955, ..., 0.48773782, 0.28792122,\n",
       "        0.51399001],\n",
       "       ...,\n",
       "       [0.80508137, 0.8482673 , 0.12417675, ..., 0.56738328, 0.50978853,\n",
       "        0.03062591],\n",
       "       [0.47694602, 0.17350347, 0.03766902, ..., 0.43732438, 0.79409029,\n",
       "        0.06445678],\n",
       "       [0.14908129, 0.929828  , 0.83326334, ..., 0.17954586, 0.05745634,\n",
       "        0.90740887]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[::-1,::] # 上下反转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "bab70cd2-1f4f-4299-af2d-a316ace4ad58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.14908129, 0.929828  , 0.83326334, ..., 0.17954586, 0.05745634,\n",
       "        0.90740887],\n",
       "       [0.47694602, 0.17350347, 0.03766902, ..., 0.43732438, 0.79409029,\n",
       "        0.06445678],\n",
       "       [0.80508137, 0.8482673 , 0.12417675, ..., 0.56738328, 0.50978853,\n",
       "        0.03062591],\n",
       "       ...,\n",
       "       [0.42209953, 0.67781044, 0.78396955, ..., 0.48773782, 0.28792122,\n",
       "        0.51399001],\n",
       "       [0.53165527, 0.3717876 , 0.7470969 , ..., 0.28631926, 0.28385361,\n",
       "        0.48664538],\n",
       "       [0.89983688, 0.47149089, 0.65183677, ..., 0.33412181, 0.369037  ,\n",
       "        0.35104955]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([arr1,arr1],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "08f1e722-e690-45ab-a148-ed5a7a224697",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.14908129, 0.929828  , 0.83326334, ..., 0.17954586, 0.05745634,\n",
       "        0.90740887],\n",
       "       [0.47694602, 0.17350347, 0.03766902, ..., 0.43732438, 0.79409029,\n",
       "        0.06445678],\n",
       "       [0.80508137, 0.8482673 , 0.12417675, ..., 0.56738328, 0.50978853,\n",
       "        0.03062591],\n",
       "       ...,\n",
       "       [0.42209953, 0.67781044, 0.78396955, ..., 0.48773782, 0.28792122,\n",
       "        0.51399001],\n",
       "       [0.53165527, 0.3717876 , 0.7470969 , ..., 0.28631926, 0.28385361,\n",
       "        0.48664538],\n",
       "       [0.89983688, 0.47149089, 0.65183677, ..., 0.33412181, 0.369037  ,\n",
       "        0.35104955]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([arr1,arr1],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "1f0fe28b-d49d-4afb-8d9e-361b9155cada",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[0.14908129, 0.929828  , 0.83326334, ..., 0.17954586, 0.05745634,\n",
       "         0.90740887],\n",
       "        [0.47694602, 0.17350347, 0.03766902, ..., 0.43732438, 0.79409029,\n",
       "         0.06445678],\n",
       "        [0.80508137, 0.8482673 , 0.12417675, ..., 0.56738328, 0.50978853,\n",
       "         0.03062591],\n",
       "        ...,\n",
       "        [0.42209953, 0.67781044, 0.78396955, ..., 0.48773782, 0.28792122,\n",
       "         0.51399001],\n",
       "        [0.53165527, 0.3717876 , 0.7470969 , ..., 0.28631926, 0.28385361,\n",
       "         0.48664538],\n",
       "        [0.89983688, 0.47149089, 0.65183677, ..., 0.33412181, 0.369037  ,\n",
       "         0.35104955]]),\n",
       " [array([[0.14908129, 0.929828  , 0.83326334, 0.76333923, 0.25012549,\n",
       "          0.27141124, 0.33765643, 0.61182269, 0.16660353, 0.63549834,\n",
       "          0.39088633, 0.60253392, 0.63859685, 0.05498834, 0.7944767 ,\n",
       "          0.0457546 , 0.48025609, 0.35134558, 0.53558842, 0.03087631,\n",
       "          0.29539992, 0.38706385, 0.22972392, 0.49367954, 0.62313401,\n",
       "          0.92133221, 0.99050499, 0.3684321 , 0.54739255, 0.13954554,\n",
       "          0.9180958 , 0.24254522, 0.60936585, 0.76869323, 0.55956983,\n",
       "          0.99865434, 0.17510738, 0.40359655, 0.55045498, 0.96731009,\n",
       "          0.28001731, 0.88235878, 0.2991464 , 0.76922462, 0.31748323,\n",
       "          0.51993464, 0.18977184, 0.17954586, 0.05745634, 0.90740887],\n",
       "         [0.47694602, 0.17350347, 0.03766902, 0.1743383 , 0.34323827,\n",
       "          0.45724813, 0.11432626, 0.88457172, 0.51464359, 0.20881309,\n",
       "          0.54027577, 0.77369524, 0.49510401, 0.10355878, 0.49907054,\n",
       "          0.66733038, 0.30421833, 0.03267722, 0.15378699, 0.96523687,\n",
       "          0.6529339 , 0.14548683, 0.56269505, 0.6706388 , 0.78708675,\n",
       "          0.99017234, 0.61300225, 0.70270605, 0.24794946, 0.13871181,\n",
       "          0.52395608, 0.43709599, 0.39269121, 0.90856807, 0.22884582,\n",
       "          0.63860534, 0.22200153, 0.69728651, 0.26399716, 0.69273431,\n",
       "          0.60021207, 0.5567836 , 0.99601217, 0.97258965, 0.39138302,\n",
       "          0.06932077, 0.38440511, 0.43732438, 0.79409029, 0.06445678]]),\n",
       "  array([[0.80508137, 0.8482673 , 0.12417675, 0.12140564, 0.3953992 ,\n",
       "          0.04467854, 0.85121233, 0.05597101, 0.48306761, 0.94074313,\n",
       "          0.49077248, 0.71211119, 0.56296104, 0.6617717 , 0.60752667,\n",
       "          0.1301308 , 0.06796948, 0.4645764 , 0.11004156, 0.9641443 ,\n",
       "          0.03069621, 0.46220451, 0.07138185, 0.49136801, 0.74876364,\n",
       "          0.43516481, 0.53290852, 0.72693351, 0.22837774, 0.45911069,\n",
       "          0.32730242, 0.1890054 , 0.3179238 , 0.74617439, 0.21619782,\n",
       "          0.87979646, 0.6246164 , 0.73173875, 0.65504924, 0.91173954,\n",
       "          0.2216428 , 0.16117746, 0.25865669, 0.48499313, 0.47203823,\n",
       "          0.23065669, 0.25578986, 0.56738328, 0.50978853, 0.03062591],\n",
       "         [0.626538  , 0.10748024, 0.10988549, 0.80257409, 0.79676985,\n",
       "          0.59167389, 0.68903652, 0.28887292, 0.59919577, 0.28914441,\n",
       "          0.78899632, 0.79838394, 0.53227136, 0.95829331, 0.10588041,\n",
       "          0.03224948, 0.48648764, 0.52146861, 0.31933259, 0.59263142,\n",
       "          0.06311283, 0.78648774, 0.16987338, 0.1196853 , 0.77133828,\n",
       "          0.20011656, 0.99258399, 0.15988752, 0.31102041, 0.75027849,\n",
       "          0.38361833, 0.80844184, 0.10054574, 0.49485065, 0.59984644,\n",
       "          0.93023292, 0.37031745, 0.70428207, 0.76454216, 0.38963968,\n",
       "          0.78221709, 0.81720926, 0.22257311, 0.94085148, 0.96054574,\n",
       "          0.64795121, 0.31606789, 0.27450452, 0.6595891 , 0.98776665],\n",
       "         [0.91777598, 0.33484731, 0.56872296, 0.13476751, 0.02733933,\n",
       "          0.98116926, 0.32504356, 0.72954086, 0.18496218, 0.11584483,\n",
       "          0.22988892, 0.88507958, 0.93296555, 0.46141833, 0.18729965,\n",
       "          0.15374989, 0.70535334, 0.04690146, 0.07749075, 0.63726416,\n",
       "          0.62260738, 0.4719135 , 0.57177025, 0.36745239, 0.91132714,\n",
       "          0.70509395, 0.77499673, 0.78283509, 0.15142144, 0.89570067,\n",
       "          0.94746538, 0.15067063, 0.84091746, 0.25385122, 0.75428193,\n",
       "          0.61502279, 0.85366659, 0.57858307, 0.28645599, 0.45152995,\n",
       "          0.8616095 , 0.15600589, 0.47773818, 0.01021917, 0.89675442,\n",
       "          0.48705068, 0.63139801, 0.68385171, 0.09484229, 0.22399652]]),\n",
       "  array([[0.82275096, 0.81968496, 0.77539171, ..., 0.96683845, 0.51240501,\n",
       "          0.71898555],\n",
       "         [0.73098313, 0.40010761, 0.64373035, ..., 0.03238484, 0.77484298,\n",
       "          0.52472032],\n",
       "         [0.57555342, 0.97800532, 0.97137099, ..., 0.44244269, 0.30817879,\n",
       "          0.24049594],\n",
       "         ...,\n",
       "         [0.42209953, 0.67781044, 0.78396955, ..., 0.48773782, 0.28792122,\n",
       "          0.51399001],\n",
       "         [0.53165527, 0.3717876 , 0.7470969 , ..., 0.28631926, 0.28385361,\n",
       "          0.48664538],\n",
       "         [0.89983688, 0.47149089, 0.65183677, ..., 0.33412181, 0.369037  ,\n",
       "          0.35104955]])])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1,np.split(arr1,[2,5],axis = 0) # 分割这个轴和拼接那个要反过来理解。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "905aae46-bd2d-4156-ba09-538f2c2984fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ————————————————————————————————————————————————————————————————————————\n",
    "# 广播：就是两个维度不匹配的array为了能够操作进行复制操作，必然存在某轴\n",
    "# 为1，不然无法广播，广播是自动进行的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "d79f294b-5a6a-4f43-b0cf-260b09cd9ed1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(30, 30)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组函数：数学函数、矩阵乘积\n",
    "np.dot(arr1,arr1.T)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
